/**
 * Storage 二次封装
 */
import { projectConfig } from '@/utils';

export default {
  getStroage(namespace: string) {
    return JSON.parse(window.localStorage.getItem(namespace) || '{}');
  },
  getItem(key: string, namespace = projectConfig.namespace) {
    return this.getStroage(namespace)[key];
  },
  setItem(key: string, val, namespace = projectConfig.namespace) {
    const storage = this.getStroage(namespace);
    storage[key] = val;
    window.localStorage.setItem(namespace, JSON.stringify(storage));
  },
  clearItem(key: string, namespace = projectConfig.namespace) {
    const storage = this.getStroage(namespace);
    delete storage[key];
    window.localStorage.setItem(namespace, JSON.stringify(storage));
  },
  clearAll() {
    window.localStorage.clear();
  },
};
